package cn.cimoc.service.impl;

import cn.cimoc.mapper.AddressMapper;
import cn.cimoc.mapper.DistrictMapper;
import cn.cimoc.model.dto.output.AddressDTO;
import cn.cimoc.model.po.TAddress;
import cn.cimoc.service.AddressService;
import cn.cimoc.util.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LGZ
 * @date 2022/7/16 14:55
 * <p>
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper mapper;

    @Autowired
    DistrictMapper dMapper;

    @Override
    public List<AddressDTO> getAddress(long uid) {
        QueryWrapper<TAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid).orderByDesc("is_default");
        List<TAddress> data = mapper.selectList(wrapper);
        return CommonUtils.getMapperFacade().mapAsList(data, AddressDTO.class);
    }

    @Override
    public void setDefault(long aid, long uid) {
        UpdateWrapper<TAddress> wrapper = new UpdateWrapper<>();
        wrapper.set("is_default", 0)
                .eq("uid", uid)
                .eq("is_default", 1);
        mapper.update(null, wrapper);

        wrapper.clear();

        wrapper.set("is_default", 1)
                .eq("aid", aid);
        mapper.update(null, wrapper);
    }

    @Override
    public void delete(long aid) {
        TAddress address = mapper.selectById(aid);
        if (address.getIsDefault() == 1L) {
            QueryWrapper<TAddress> wrapper = new QueryWrapper<>();
            wrapper.eq("uid", address.getUid())
                    .orderByDesc("modified_time")
                    .last("limit 1");
            TAddress minAddress = mapper.selectOne(wrapper);
            if (null != minAddress) {
                minAddress.setIsDefault(1L);
                UpdateWrapper<TAddress> uw = new UpdateWrapper<>();
                uw.eq("aid", minAddress.getAid());
                mapper.update(minAddress, uw);
            }
        }
        mapper.deleteById(aid);
    }

    @Override
    public void add(String receiver, String provinceCode, String cityCode, String areaCode, String zip, String address, String phone, String tel, String tag, String username, long uid) {
        TAddress tAddress = new TAddress();
        tAddress.setReceiver(receiver);
        tAddress.setProvinceCode(provinceCode);
        tAddress.setProvinceName(dMapper.getNameByCode(provinceCode));
        tAddress.setCityCode(cityCode);
        tAddress.setCityName(dMapper.getNameByCode(cityCode));
        tAddress.setAreaCode(areaCode);
        tAddress.setAreaName(dMapper.getNameByCode(areaCode));
        tAddress.setZip(zip);
        tAddress.setAddress(address);
        tAddress.setPhone(phone);
        tAddress.setTel(tel);
        tAddress.setTag(tag);
        tAddress.setUid(uid);
        tAddress.setCreatedUser(username);
        tAddress.setModifiedUser(username);
        QueryWrapper<TAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        tAddress.setIsDefault(mapper.selectCount(wrapper) == 0 ? 1L : 0L);
        mapper.insert(tAddress);
    }
}
